package cn.smile.config.shiro.filter;

import cn.smile.config.shiro.CasPathConfig;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.cas.CasFilter;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

@Log4j2
public class MyCasFilter extends CasFilter {
    public MyCasFilter() {
        this.setName("casFilter");
        this.setEnabled(true);
        //登录的URL
        this.setLoginUrl(CasPathConfig.LOGIN_URL);
        //登录成功跳转的URL
        this.setSuccessUrl("/index");
        //认证时失败的URL
        this.setFailureUrl("/failure");
    }

    @Override
    protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
        log.info("MyCasFilter - onLoginSuccess");
        return super.onLoginSuccess(token, subject, request, response);
    }

    @Override
    protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException ae, ServletRequest request, ServletResponse response) {
        log.info("MyCasFilter - onLoginFailure");
        return super.onLoginFailure(token, ae, request, response);
    }

    /**
     * doGetAuthenticationInfo抛出AuthenticationException时，会回调该方法
     *
     * @param request
     * @param response
     * @param mappedValue
     * @return
     * @throws Exception
     */
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        log.info("MyCasFilter - onAccessDenied");
        return super.onAccessDenied(request, response, mappedValue);
    }
}
